require('../../mypublic/css/common.less');
require('../../mypublic/css/index.less');

require('../../public/misc/mediaUpload/css/style.css');
require('../../public/misc/jquery.jqGrid-4.6.0/css/ui.jqgrid.css');
require('../../public/misc/jquery-ui-themes-1.11.2/humanity/jquery-ui.min.css');


require("jquery");

require('../../public/misc/jquery.jqGrid-4.6.0/js/i18n/grid.locale-cn');
require('../../public/misc/jquery.jqGrid-4.6.0/js/jquery.jqGrid.min');

require('../../public/js/myservice/side');
require("../../public/js/myservice/authorize");

oriFunc();

function oriFunc() {
    $(document).ready(function () {
        $('#search-btn').bind('click', function () {
            var keyword = $('#search-keyword').val();

            $(this).attr('disabled', 'disabled');
            $('#grid-table').setGridParam(
                {
                    postData: {
                        keyword: keyword
                    }
                }
            );
            $('#grid-table').jqGrid('setGridParam', {
                datatype: 'json',
                page: 1
            }).trigger('reloadGrid');
            $('#search-keyword').focus();
        });

        $('#search-keyword').bind('keypress', function (e) {
            if (e.keyCode == 13) {
                $('#search-btn').trigger('click');
                return false;
            }
            else if (e.keyCode == 27) {
                $('#clean-keyword-btn').trigger('click');
                $(this).val('').blur().focus();
                return false;
            }
        }).focus();

        $('#add-btn').bind('click', function () {
            window.location = "/cms/article_edit/index.html"
        });

        $('#release-btn').bind('click', function () {
            $(this).attr('disabled', 'disabled');
            setBatch('status', 1);
            $('#grid-table').trigger('reloadGrid');
        });

        $('#cancel-release-btn').bind('click', function () {
            $(this).attr('disabled', 'disabled');
            setBatch('status', 0);
            $('#grid-table').trigger('reloadGrid');
        });

        $('#delete-btn').bind('click', function () {
            $(this).attr('disabled', 'disabled');
            setBatch('del', 1);
            $('#grid-table').trigger('reloadGrid');
        });

        function setBatch(section, val) {
            var list = getList();
            $('#grid-table').setGridParam({
                postData: {
                    ids: list,
                    update: section,
                    value: val
                }
            });
        }

        function getList() {
            var rowData = jQuery('#grid-table').jqGrid('getGridParam', 'selarrrow');
            var list = "";
            if (rowData.length) {
                for (var i = 0; i < rowData.length; i++) {
                    list += ':' + rowData[i];
                }
            }
            return list;
        }

        $('#grid-table').jqGrid(
            {
                url: '/api/index.php?r=/cms/article/get-list',
                colNames: [
//                '#编号',
//                '标题',
//                '关键字',
//                '点赞数',
//                '附件下载数',
//                '发布状态',
//                '分类',
//                '排序',
//                '修改时间',
//                '修改人',
//                '操作'
                    '编号',
                    '标题',
                    '作者',
                    '标签',
                    '收藏数',
                    '附件下载数',
                    '浏览量',
                    '对内发布',
                    '目录',
                    '排序',
                    '编辑时间',
                    '编辑人',
                    '对外发布'
                ],
                colModel: [
                    {index: 'id', name: 'aId', align: 'center', width: 20},
                    {index: 'title', name: 'title', align: 'center', width: 70, editable: true},
                    {index: 'author', name: 'author', align: 'center', width: 40},
                    {index: 'tag', name: 'tag', align: 'center', width: 40, editable: true},
//                { index: 'pv', name: 'pv', align: 'center', width: 25 },down_pv
                    {index: 'collect', name: 'collect', align: 'center', width: 30},
                    {index: 'down_pv', name: 'down_pv', align: 'center', width: 30},
                    {index: 'scan', name: 'scan', align: 'center', width: 30},
                    {
                        index: 'inside',
                        name: 'inside',
                        align: 'center',
                        width: 30,
                        classes: 'status',
                        editable: true,
                        edittype: 'select',
                        editoptions: {value: "0:未发布;1:已发布"}
                    },
                    {index: 'dirName', name: 'dirName', align: 'center', width: 30},
                    {index: 'sort', name: 'sort', align: 'center', width: 20, editable: true},
                    {index: 'modifiedTime', name: 'modifiedTime', align: 'center', width: 50},
                    {index: 'modifiedBy', name: 'modifiedBy', align: 'center', width: 30},
                    {
                        index: 'outside',
                        name: 'outside',
                        align: 'center',
                        width: 30,
                        classes: 'status',
                        editable: true,
                        edittype: 'select',
                        editoptions: {value: "0:未发布;1:已发布"}
                    },
                    //{ index: 'edit', name: 'edit', align: 'center', width: 90 }
                ],
                datatype: 'json',
                height: 'auto',
                autowidth: true,
                rowNum: 20,
                rowList: [1, 10, 20, 50, 100],
                viewrecords: true,
                viewsortcols: 'vertical',
                hoverrows: true,

                pager: $('#grid-pager'),
                sortname: 'id',
                sortorder: 'desc',
                multiselect: true,
                cellEdit: true,
                cellurl: '/api/index.php?r=/cms/article/quick-set',
                afterSubmitCell: function (serverresponse, rowid, cellname, value, iRow, iCol) {
                    if ('' != serverresponse.responseText) {
                        var response = $.parseJSON(serverresponse.responseText);
                        return [response.status, response.content];
                    } else return [true];
                },
                loadComplete: function () {
                    $('#search-btn').attr('disabled', false);
                    $('#release-btn').attr('disabled', false);
                    $('#cancel-release-btn').attr('disabled', false);
                    $('#delete-btn').attr('disabled', false);
                },
                ondblClickRow: function (id) {
                    window.location.href = '/cms/article_edit/index.html?id=' + id;
                },
                onSelectRow: function (id) {

                }
            }
        ).navGrid(
            '#grid-pager',
            // options
            {
                search: false,
                addfunc: function () {
                    window.location.href = '/cms/article_edit/index.html';
                },
                editfunc: function (id) {
                    window.location.href = '/cms/article_edit/index.html&id=' + id;
                }

            },
            // edit
            {
                url: '/cms/article_edit/index.html',
                beforeSubmit: function (postData) {
                    console.log(postData)
                    return;
                }
            },
            // add
            {},
            // del
            {
                url: '/api/index.php?r=/cms/article/delete'
            }
        );
    });
    window.updateStatus = function (id, type, obj, fieldType) {
        var url = {1: '/api/index.php?r=/cms/article/set-top', 2: '/api/index.php?r=/cms/article/release'};
        $.post(url[fieldType], {'id': id, 'type': type}, function (data) {
            if (type == 1) {
                if (data.success == 1) {
                    fieldType == 2 && obj.parent().parent().find('.status').text('已发布');
                    fieldType == 1 && obj.parent().parent().find('.betop').text('是');
                    alert('设置成功！');
                } else {
                    alert('设置失败！');
                }
            } else {
                if (data.success == 1) {
                    fieldType == 2 && obj.parent().parent().find('.status').text('未发布');
                    fieldType == 1 && obj.parent().parent().find('.betop').text('否');
                    alert('设置成功！');
                } else {
                    alert('设置失败！');
                }
            }
        }, 'json');
    }
}